##
# Entity
#
# Description: The base class for all objects on the map
#
# Since: 12-8-2013
#

import libtcodpy

class Entity:

    def __init__(self, health,
                 icon=" ", textColor=libtcodpy.white, backgroundColor=libtcodpy.black,
                 position=None,  name="", blocksMovement=True, blocksVision=False,
                 allegiance=0,
                 ai=None, resistance=None, attack=None, actions=None, inventory=None):

        # The name of the entity to be displayed to the player
        self.name = name

        # The team that the entity will be on. 0 = No Team, 1 = Player Team
        self.allegiance = allegiance

        # The amount of health points the Entity currently has
        self.health = health
        # The maximum amount of health points the entity can have
        self.maxHealth = health

        # The position of the entity on the map
        self.position = position

        # Weather this entities blocks movement of other entities
        self.blocksMovement = blocksMovement

        # Weather this entity blocks the sight of other entities
        self.blocksVision = blocksVision

        # The ascii character to represent this object on the map
        self.icon = icon
        # The color to draw the icon
        self.textColor = textColor
        # Background color for the tile
        self.backgroundColor = backgroundColor

        # The modules that this entity has
        # Example: AI, Health, Inventory
        self.modules = {}

        # The AI controlling the entity
        if ai:
            self.modules["ai"] = ai

        # Resistance to different types of damage
        if resistance:
            self.modules["resistance"] = resistance

        # The types of attacks this entity can preform
        if attack:
            self.modules["attack"] = attack

        # The types of actions this entity can preform
        if actions:
            self.modules["actions"] = actions

        # The inventory consisting of the items this entity is holding
        if inventory:
            self.modules["inventory"] = inventory

        return

    ##
    # onTick
    #
    # Description: Calls the onTick method of every module whenever the game clock ticks
    #
    def onTick(self):

        for key in self.modules:

            # Lets make sure it actually has a onTick method so we don't crash stuff
            if hasattr(self.modules[key], "onTick"):
                # Cool it does, no lets onTick!
                self.modules[key].onTick()

    ##
    # onTouch
    #
    # Description: Calls the onTick method of every module whenever another entity touches this one.
    #   Calls the components on touch method with the same arguments as this one received
    #
    # Parameters:
    #   toucher - the entity that touched me
    def onTouch(self, toucher):

        for key in self.modules:

            # Lets make sure it actually has a onTouch method so we don't crash stuff
            if hasattr(self.modules[key], "onTouch"):
                # Cool it does, no lets onTouch!
                self.modules[key].onTouch(toucher)

    ##
    # onDamage
    #
    # Description: Calls the onDamage method of every module whenever another entity touches this one.
    #   Calls the components on touch method with the same arguments as this one received
    #
    # Parameters:
    #   attacker - the entity that touched me
    def onDamage(self, attacker):
        for key in self.modules:

            # Lets make sure it actually has a onTouch method so we don't crash stuff
            if hasattr(self.modules[key], "onDamage"):
                # Cool it does, no lets onDamage!
                self.modules[key].onDamage(attacker)